<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class DQ_Model extends CI_Model 
{
    public function __construct()
    {
        parent::__construct();
        $this->error_code = $this->config->item('error_code');
    }

    protected function checkUser()
    {
        $user_id = $this->session->userdata('uid');

        if(empty($user_id))
        {
            echo json_encode(array('error'=>100,'msg'=>$this->error_code[100]));
            exit;
        }
        else
        {
            return $user_id;
        }
    }

    protected function checkInput($formName,$get=1)
    {
        $this->load->library('validation');

        try
        {
            $data = $get == 1 ? $this->input->get(NULL) : $this->input->post(NULL);
            $checkedData = $this->validation->checkFormData($data,$formName);
        }
        catch (Exception $e)
        {
             if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
             {
                 echo json_encode(array('error'=>$e->getCode(),'msg'=>$e->getMessage()));
                 exit;
             }
             else
             {
                error_page($e->getMessage());
             }
        }
        
        return $checkedData;

    }

    protected function _checkCaptcha()
    {
        $code = $this->session->userdata('ccode');
        
        if( md5(strtolower($this->input->post('captcha'))) == $code)
        {
            $this->session->unset_userdata('ccode');
            return 0;
        }
        else
        {
            return array('error'=>2014,'msg'=>$this->error_code[2014],'data'=>array());
        }
    }

}
